package cn.iocoder.yudao.module.jifenyuan.service.notify;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.push.core.utils.PushUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.roomuserbind.RoomUserBindDO;
import cn.iocoder.yudao.module.jifenyuan.enums.JifenyuanRoomEventTypeEnum;
import cn.iocoder.yudao.module.jifenyuan.enums.JifenyuanWalletLogChangeTypeEnum;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.vo.PushMsgVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotifyServiceImpl implements NotifyService{
    @Value("${jifenyuan.push.mp}")
    private String mpNotifyUrl;
    @Value("${jifenyuan.push.web}")
    private String webNotifyUrl;
    @Resource
    private MemberUserApi memberUserApi;


    @Override
    @Async
    public void notifyPartner(List<Long> userIds,Long fromUserId,Long toUserId, Long score, JifenyuanRoomEventTypeEnum type) {
//        PushMsgVo pushMsgVo = new PushMsgVo();
//        pushMsgVo.setPushUrl(mpNotifyUrl);
//        pushMsgVo.setUserIds(userIds);
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type",type.getStatus());
        jsonObject.set("from",fromUserId);
        jsonObject.set("to",toUserId);
        jsonObject.set("score",score);
//        pushMsgVo.setPayload(jsonObject.toString());
//        memberUserApi.pushMsg(pushMsgVo);

        List<String> cids = memberUserApi.getCidsByUserIds(userIds);
        PushUtil.getInstance().push(mpNotifyUrl,cids, jsonObject.toString());

    }

    @Override
    @Async
    public void notifyObserver(List<String> cids, Long fromUserId, Long toUserId, Long score, JifenyuanRoomEventTypeEnum type) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type",type.getStatus());
        jsonObject.set("from",fromUserId);
        jsonObject.set("to",toUserId);
        jsonObject.set("score",score);
        PushUtil.getInstance().push(mpNotifyUrl,cids,jsonObject.toString());
    }

    @Override
    public void notifyWebObserver(List<String> cids, Long fromUserId, Long toUserId, Long score, JifenyuanRoomEventTypeEnum type) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type",type.getStatus());
        jsonObject.set("from",fromUserId);
        jsonObject.set("to",toUserId);
        jsonObject.set("score",score);
        PushUtil.getInstance().push(webNotifyUrl,cids,jsonObject.toString());
    }

    @Override
    @Async
    public void notifyCoinChange(List<String> cids, Long score, JifenyuanRoomEventTypeEnum type, JifenyuanWalletLogChangeTypeEnum coinChangeType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("type",type.getStatus());
        jsonObject.set("score",score);
        jsonObject.set("coinChangeType",coinChangeType.getStatus());
        PushUtil.getInstance().push(mpNotifyUrl,cids,jsonObject.toString());
    }

    @Override
    @Async
    public void notify(List<Long> userIds, String payload) {
        List<String> cids = memberUserApi.getCidsByUserIds(userIds);
        PushUtil.getInstance().push(mpNotifyUrl,cids,payload);
    }

    @Override
    @Async
    public void notifyCids(List<String> cids, String payload) {
        PushUtil.getInstance().push(mpNotifyUrl,cids,payload);
    }
    @Override
    @Async
    public void notifyWebCids(List<String> cids, String payload) {
        PushUtil.getInstance().push(webNotifyUrl,cids,payload);
    }
}
